#include "udm.h"

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	if(htim == &htim8 && htim ->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
	{
		upEdge = HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_3);
		downEdge = HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_4);
		udm_distance = (downEdge - upEdge)*0.034/2;		//back-start之间计数器值除音速除2
	}
}
/**
  * @brief 超声波定时器初始化
  * @retval None
  */
void udm_init(void)
{
	HAL_TIM_Base_Start(&htim8);
	HAL_TIM_IC_Start(&htim8, TIM_CHANNEL_3);
	HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_4);
}

void measureDistance(void)
{
	//向trig引脚发送脉冲触发模块发出超声波
	HAL_GPIO_WritePin(trig_GPIO_Port,trig_Pin,GPIO_PIN_SET);
	HAL_Delay(1);
	HAL_GPIO_WritePin(trig_GPIO_Port,trig_Pin,GPIO_PIN_RESET);
	__HAL_TIM_SetCounter(&htim8, 0);
	HAL_Delay(20);
}	
